package top.hzy520.email.service;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.hzy520.common.exception.YuYuBuyException;
import top.hzy520.common.util.RandomNumber;
import top.hzy520.common.util.RedisUtil;
import top.hzy520.jms.MessageProcessor;

import javax.jms.Destination;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: HouZeYu
 * @Description: 邮箱接口业务
 * @Date: Created in 20:46 2018/5/29
 */
@Slf4j
@Service
public class EamilServiceImpl implements EmailService {

    private static final String EMAIL_QUEUE = "email.queue";
    @Autowired
    private RedisUtil redisUtil;
     @Autowired
     private MessageProcessor messageProcessor;
    @Override
    public  void sendMail(String email){
        String code=RandomNumber.getRandomVerifyCode();
        Destination destination=new ActiveMQQueue(EMAIL_QUEUE);
        Map<String,String> param=new HashMap<>();
        param.put("email",email);
        param.put("code",code);
        messageProcessor.sendMesasageToQueue(destination,JSON.toJSONString(param));
    }
    @Override
    public boolean verifyEmailCode(String email, String code) {
      String verifyCode=redisUtil.getEmailVerifyCode(email);
      if (verifyCode.equals(code)){
             return true;
      }else{
          log.info(email+"的用户:验证码不正确");
         return false;
      }
    }
}
